package de.hpi.generator.generator;

import de.hpi.generationmanager.GenerationManager;
import de.hpi.generator.config.ConfigParser;
import de.hpi.generator.helpers.FileJoiner;
import de.hpi.generator.model.Database;

public class GeneratorMain {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {

        try {
            StartArgs aStartArgs = new StartArgs(args);
            System.out.println("Parsing configuration file: " + aStartArgs.getConfigPath());
            Database aDatabase = prepareDatabaseToGeneration(aStartArgs, args);

            System.out.println("Start data generation");
            long generationStart = System.currentTimeMillis();
            aDatabase.generateData();
            System.out.println(String.format("The generation took: %d sec", (System.currentTimeMillis() - generationStart) / 1000));

            System.out.println("Creating output files");
            long outputCreatorStart = System.currentTimeMillis();
            FileJoiner.compileCSVFromGeneratedData(aDatabase);
            System.out.println(String.format("Creating of output files took: %d sec", (System.currentTimeMillis() - outputCreatorStart) / 1000));

            System.out.println("Your database was generated!");

 //           printSurprise();

        } catch (WrongParamsException e) {
            showUsages();
        } catch (Exception e) {
            System.err.println("ERROR: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static Database prepareDatabaseToGeneration(StartArgs aStartArgs, String[] args) throws Exception {
        Database aDatabase;
        if (aStartArgs.isIntegratedMode()) {
            aDatabase = GenerationManager.preProcessIntergratedConfig(args);
        } else {
            aDatabase = ConfigParser.parseConfigFile(aStartArgs);
        }
        return aDatabase;
    }

    private static void printSurprise() {
        System.out.println("                             ;\\ \n"
                + "                            |' \\ \n"
                + "         _                  ; : ; \n"
                + "        / `-.              /: : | \n"
                + "       |  ,-.`-.          ,': : | \n"
                + "       \\  :  `. `.       ,'-. : | \n"
                + "        \\ ;    ;  `-.__,'    `-.| \n"
                + "         \\ ;   ;  :::  ,::'`:.  `. \n"
                + "          \\ `-. :  `    :.    `.  \\ \n"
                + "           \\   \\    ,   ;   ,:    (\\ \n"
                + "            \\   :., :.    ,'o)): ` `-. \n"
                + "           ,/,' ;' ,::''`.`---'   `.  `-._ \n"
                + "         ,/  :  ; ''      `;'          ,--`. \n"
                + "        ;/   :; ;             ,:'     (   ,:) \n"
                + "          ,.,:.    ; ,:.,  ,-._ `.     \'''/ \n"
                + "          '::'     `:'`  ,'(  \\`._____.-''' \n"
                + "             ;,   ;  `.  `. `._`-.  \\ \n"
                + "             ;:.  ;:       `-._`-.\\  \\`. \n"
                + "              '`:. :        |' `. `\\  ) \\ \n"
                + "                 ` ;:       |    `--\\__,' \n"
                + "                   '`      ,' \nGikki Wirkir!"
                + "                        ,-");
    }

    private static void showUsages() {
        System.out.println("");
        System.out.println("To start data generation please use following command:");
        System.out.println("");
        System.out.println("runGenerator.bat typeOfConfig configFile.json output_path");
        System.out.println("");
        System.out.println("where:");
        System.out.println("    typeOfConfig    - type of configuration file");
        System.out.println("        can be:");
        System.out.println("            integrated_config - for configuration files ");
        System.out.println("                                which includes tesma ");
        System.out.println("                                directives");
        System.out.println("            ordinary_config   - for configuration file ");
        System.out.println("                                of simple data generation");
        System.out.println("");
        System.out.println("    configFile.json - a configuration file which describes ");
        System.out.println("	                how data shall be generated");
        System.out.println("					  ");
        System.out.println("    output_path     - a output path of data for database");
        System.out.println("");
    }
}
